﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using MongoDB.Driver;
using MongoServices.Tests.TestHelpers;

namespace MongoServices.Tests
{
	public class TestBase : IDisposable
	{
		protected const string COLLECTION_NAME_TEST_MODEL_SIMPLE = "TestModelSimple";

		protected MongoDatabase Database { get; private set; }
		protected MongoCollection<TestModelSimple> CollectionTestModelSimple { get; private set; }

		public TestBase()
		{
			Setup();
		}

		private void Setup()
		{
			SetupProperties();
			ResetDatabase();
		}

		private void SetupProperties()
		{
			Database = MongoDatabase.Create(ConfigurationManager.ConnectionStrings["MongoServiceConnectionString"].ConnectionString);
			CollectionTestModelSimple = Database.GetCollection<TestModelSimple>(COLLECTION_NAME_TEST_MODEL_SIMPLE);
		}

		private void DropCollection(string collectionName)
		{
			if (Database.CollectionExists(collectionName))
			{
				Database.DropCollection(COLLECTION_NAME_TEST_MODEL_SIMPLE);
			}
		}

		private void ResetDatabase()
		{
			DropCollection(COLLECTION_NAME_TEST_MODEL_SIMPLE);
		}

		public virtual void Dispose()
		{
			ResetDatabase();
		}
	}
}
